System.trace( "event.lua begin" )
gPbcPath = System.getPbcPath()
parser.register("head.proto", gPbcPath.."/../")
parser.register("gate.proto", gPbcPath)
parser.register("login.proto", gPbcPath)
gConnectServerState = false

function onConnect(  )
	System.trace( "connect gate server" )
	gConnectServerState = true
	testAccountLogin(  )
end

function onRecv( headBuf, bodyBuf )
	local head = protobuf.decode( "head.Head" , headBuf )
	
	
	-- System.trace( "head.srcID".. tostring( head.srcID ) )
	-- System.trace( "head.type".. tostring( head.type ) )
	-- System.trace( "head.protoID".. tostring( head.protoID ) )
	-- System.trace( "body.result".. tostring( body.result ) )
	-- System.trace( "body.userID".. tostring( body.userID ) )
	if head.protoID == 100 then
		local body = protobuf.decode( "gate.Ping" , bodyBuf )
		local index = body.result + 1
		if index > 10000 then
			System.trace(os.time())
			return
		end
		test1( index )
	else
		local body = protobuf.decode( "login.LoginRsp" , bodyBuf )
		test1( 1 )
		System.trace(os.time())
	end
end

-- local addressbook1 = {
-- 	name = "Alice",
-- 	id = 123456789123,
-- 	phone = {
-- 		{ number = "1301234567" },
-- 		{ number = "8765432111111", type = "WORK" },
-- 		{ number = "13912345678", type = "MOBILE" },
-- 	},
-- 	email = "username@domain.com"
-- }

-- local addressbook2 = {
-- 	name = "Bob",
-- 	id = 12346,
-- 	phone = {
-- 		{ number = "1301234568" },
-- 		{ number = "98765432", type = "HOME" },
-- 		{ number = "13998765432", type = "MOBILE" },
-- 	}
-- }
	
-- 	local code1 = protobuf.encode("tutorial.Person", addressbook1)
-- 	--code2 = protobuf.encode("tutorial.Person", addressbook2)
-- 	local decode1 = protobuf.decode("tutorial.Person" , code1)
-- 	System.trace( "addressbook1.id" .. addressbook1.id )
-- 	System.trace( "decode1.id" .. decode1.id )


-- 建立成功后进行账号登陆

function test1( index )
	local headTable = { srcID = 0, desID = 0, type="PROTOBUF", protoID = 100 }
	local codeHead = protobuf.encode("head.Head", headTable)
	local accountTable = { result = index }
	local codeAccount = protobuf.encode("gate.Ping", accountTable)
	--for i=1, 100 do 
		System.send( codeHead, codeAccount )
	--end
end


function testAccountLogin(  )
	local headTable = { srcID = 0, desID = 0, type="PROTOBUF", protoID = 11 }
	local codeHead = protobuf.encode("head.Head", headTable)
	local accountTable = { accountName = "hanchen" }
	local codeAccount = protobuf.encode("login.LoginReq", accountTable)
	
	--for i=1, 100 do 
		System.send( codeHead, codeAccount )
	--end
end


-- local head = {
-- 	srcID = 1,
-- 	desID = 1,
-- 	type="PROTOBUF",
-- 	protoID= 100
-- }

-- local code1 = protobuf.encode("head.Head", head)
-- local decode1 = protobuf.decode("head.Head" , code1)
-- System.trace( "decode1.srcID"..decode1.srcID )
-- System.trace( "decode1.desID"..decode1.desID )
-- System.trace( "decode1.type"..decode1.type )
-- System.trace( "decode1.protoID"..decode1.protoID )




-- -- BUG [ISSUE#27](https://github.com/cloudwu/pbc/issues/27)
-- decode1.profile.nick_name = "AHA"
-- decode1.profile.icon = "id:1"

-- decode2 = protobuf.decode("tutorial.Person" , code2)

-- function print_addr(decoded)
-- 	System.trace(string.format('ID: %d, Name: %s, Email: %s', decoded.id, decoded.name, tostring(decoded.email)))
-- 	if decoded.profile then
-- 		System.trace(string.format('\tNickname: %s, Icon: %s', tostring(decoded.profile.nick_name), tostring(decoded.profile.icon)))
-- 	end
-- 	for k, v in ipairs(decoded.phone) do
-- 		System.trace(string.format("\tPhone NO.%s: %16s %s", k, v.number, tostring(v.type)))
-- 	end
-- end

-- print_addr(decode1)
-- print_addr(decode2)

-- buffer = protobuf.pack("tutorial.Person name id", "Alice", 123)
-- System.trace(protobuf.unpack("tutorial.Person name id", buffer))

-- code_phone = protobuf.encode("tutorial.Person.PhoneNumber", { number = "18612345678" })
-- decode_phone = protobuf.decode("tutorial.Person.PhoneNumber" , code_phone)
-- System.trace(string.format("Phone: %16s %s", decode_phone.number, tostring(decode_phone.type)))
System.trace( "event.lua end" )